package org.xc.xg;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.tencent.android.tpush.XGIOperateCallback;
import com.tencent.android.tpush.XGPushConfig;
import com.tencent.android.tpush.XGPushManager;

/**
 * Created by Administrator on 2018/9/21.
 */

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        checkPermissions();

    }

    private void checkPermissions() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.READ_PHONE_STATE},1);
        } else {
            init();
        }
    }

    private void init() {
        //信鸽日志
        XGPushConfig.enableDebug(this, true);
//        XGPushConfig.setHuaweiDebug(true);
        //打开第三方推送
//        XGPushConfig.enableOtherPush(getApplicationContext(),true);
        //
        XGPushManager.registerPush(getApplicationContext(), "18907186852", new XGIOperateCallback() {
            @Override
            public void onSuccess(Object o, int i) {
                Log.e("registerPush","onSuccess-->" + o.toString());
                XGPushConfig.getToken(getApplicationContext());
            }

            @Override
            public void onFail(Object o, int i, String s) {
                Log.e("registerPush","onFail-->" + o.toString());
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == 1) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                init();
            }
        }
    }
}
